<template>
  <!-- 审批管理-我的审核-页面 -->
  <section class="page own-approval">
    <data-form :model="ownModel" @onSearch="refreshData" :page="pageService" ref="ownModel" :rules="rules">
      <template slot="inputs">
        <el-form-item label="关联ID" prop="relationId">
          <el-input v-model.trim="ownModel.relationId" clearable></el-input>
        </el-form-item>
        <el-form-item label="审批类型" prop="applyCategory">
          <el-select v-model="ownModel.applyCategory" size="small" clearable>
            <el-option v-for="{ index, name, code } in $dict.getDictData(
                'ApplyCategory'
              )" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请人" prop="operatorCode">
          <el-input v-model.trim="ownModel.operatorCode" maxlength="20" clearable></el-input>
        </el-form-item>
        <el-form-item label="审批状态" prop="applyStatus">
          <el-select v-model="ownModel.applyStatus" size="small" clearable>
            <el-option label="--所有--" value="" />
            <el-option v-for="{ index, name, code } in $dict.getDictData('ApplyStatus')" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="ownDataSet" @onPageChange="refreshData" :page="pageService">
      <template slot="columns">
        <el-table-column label="操作" :min-width="$helper.getColumnWidth(3)">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="approvalClick(scope, '查看')">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="applyCategory" label="审批类型" :formatter="
            row => $filter.dictConvert(row.applyCategory, 'ApplyCategory')
          " :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="关联ID" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip prop="relationId">
          <!-- <template slot-scope="scope">
            <span @click="approvalClick(scope, '查看')" class="about-id">{{ scope.row.relationId }}</span>
          </template> -->
        </el-table-column>
        <el-table-column prop="operatorCode" label="申请人" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="applyDescription" label="申请原因" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="applyTime" label="申请时间" :formatter="row => $filter.dateTimeFormat(row.applyTime)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="applyStatus" label="审批状态" :formatter="
            row => $filter.dictConvert(row.applyStatus, 'ApplyStatus')
          " :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
      </template>
    </data-box>
    <el-dialog :title="$dict.getDictName(rowData.applyCategory, 'ApplyCategory') + title" :visible.sync="dialog.leaveApproval" width="1100px" :close-on-click-modal="false" @close="
        $refs['account-approval-common'].reset();
        rowData = '';
        refreshData();
      ">
      <!-- 账户审批 -->
      <account-approval-common ref="account-approval-common" :rowData="rowData" @close="dialog.leaveApproval = false; refreshData(); " v-if="rowType === 'CASE_ADJUSTMENT' || rowType === 'PAUSE_TEL' || rowType === 'PAUSE_SMS' || rowType === 'STOP_CASE' || rowType === 'PAUSE_CASE' || rowType === 'LEAVE_CASE' || rowType === 'AGENT_APPLY' || rowType === 'CASE_BACK' || rowType === 'DERATE_CASE' || this.rowType === 'ACTIVE_SMS' || this.rowType === 'ACTIVE_TEL' || this.rowType === 'PAUSE_CASE_ACTIVE'" :recordHistory="recordHistory" :rowId="rowId" :rowName="rowName" :rowType="rowType" :type="type"></account-approval-common>
      <!-- 标签审批  -->
      <tag-approval-common ref="account-approval-common" :rowData="rowData" @close="
          dialog.leaveApproval = false;
          refreshData();
        " v-if="rowType === 'TAG_CREATE'" :recordHistory="recordHistory" :rowId="rowId" :rowName="rowName" :type="type"></tag-approval-common>
      <!-- 标签调整审批 -->
      <tag-tune-approval-common ref="account-approval-common" :rowData="rowData" @close="
          dialog.leaveApproval = false;
          refreshData();
        " v-if="rowType === 'TAG_ACCOUNT' || rowType === 'TAG_USER'" :nameType="$dict.getDictName(rowData.applyCategory, 'ApplyCategory')" :recordHistory="recordHistory" :rowId="rowId" :rowName="rowName" :type="type" :rowType="rowType"></tag-tune-approval-common>
      <!-- 新建案件类型调整类型审批 -->
      <case-type-common ref="account-approval-common" :rowData="rowData" @close="
          dialog.leaveApproval = false;
          refreshData();
        " v-if="rowType === 'CASE_CREATE_ADJUSTMENT'" :recordHistory="recordHistory" :rowId="rowId" :rowName="rowName" :type="type"></case-type-common>
      <!-- 队列审批 -->
      <queue-approval-common ref="account-approval-common" :rowData="rowData" @close="
          dialog.leaveApproval = false;
          refreshData();
        " v-if="isQueue" :recordHistory="recordHistory" :rowId="rowId" :rowType="rowType" :rowName="rowName" :type="type"></queue-approval-common>
      <!-- 策略审批 -->
      <policy-approval-common ref="account-approval-common" :rowData="rowData" @close="
          dialog.leaveApproval = false;
          refreshData();
        " v-if="isPolicy" :recordHistory="recordHistory" :rowId="rowId" :rowType="rowType" :rowName="rowName" :type="type"></policy-approval-common>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import { State, Getter } from "vuex-class";
import { CaseApplyRecordService } from "~/services/domain-service/case-apply-record.service";
import accountApprovalCommon from "~/components/approval-manage/own-approval/account-approval-common.vue";
import tagApprovalCommon from "~/components/approval-manage/own-approval/tag-approval-common.vue";
import tagTuneApprovalCommon from "~/components/approval-manage/own-approval/tag-tune-approval-common.vue";
import caseTypeCommon from "~/components/approval-manage/own-approval/case-type-common.vue";
import queueApprovalCommon from "~/components/approval-manage/own-approval/queue-approval-common.vue";
import policyApprovalCommon from "~/components/approval-manage/own-approval/policy-approval-common.vue";
import { UserTagService } from "~/services/domain-service/user-tag-controller.service";
import { CaseTagAdjustApplyServer } from "~/services/domain-service/case-tag-adjust-apply.service";
import { PersonalTagAdjustApplyServer } from "~/services/domain-service/personal-tag-adjust-apply.service";
import { CaseTagTypeApplyService } from "~/services/domain-service/case-tag-type-apply.service";
import { QueueService } from "~/services/domain-service/queue-manage.service";
import { QueueLightService } from "~/services/domain-service/queue-light.service";
import { EntityIdService } from "~/services/management-service/entity-id.service";
import { RuleEliminateStrategy } from "~/services/domain-service/rule-eliminate-strategy.service";
import { NameListEliminateStrategyService } from "~/services/domain-service/name-list-eliminate-strategy.service";
import { DateStrategyService } from "~/services/management-service/date-strategy.service";
import { CustomerGroupManageService } from "~/services/domain-service/customer-group-manage.service";
import { CaseInfoService } from "~/services/domain-service/case-info.service";
import { CasePauseActiveService } from "~/services/domain-service/case-pause-active.service"

@Auth(1701)
@Layout("workspace")
@Component({
  components: {
    accountApprovalCommon,
    tagApprovalCommon,
    tagTuneApprovalCommon,
    caseTypeCommon,
    queueApprovalCommon,
    policyApprovalCommon
  }
})
export default class OwnApproval extends Vue {
  @Dependencies(CasePauseActiveService) private CasePauseActiveService: CasePauseActiveService
  @Dependencies(CaseInfoService) private CaseInfoService: CaseInfoService;
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(DateStrategyService)
  private DateStrategyService: DateStrategyService;
  @Dependencies(SortService) private sortService: SortService;
  @Dependencies(CaseApplyRecordService)
  private caseApplyRecordService: CaseApplyRecordService;
  @Dependencies(UserTagService) private UserTagService: UserTagService;
  @Dependencies(CaseTagAdjustApplyServer)
  private CaseTagAdjustApplyServer: CaseTagAdjustApplyServer;
  @Dependencies(PersonalTagAdjustApplyServer)
  private PersonalTagAdjustApplyServer: PersonalTagAdjustApplyServer;
  @Dependencies(CustomerGroupManageService)
  private CustomerGroupManageService: CustomerGroupManageService;
  @Dependencies(CaseTagTypeApplyService)
  private CaseTagTypeApplyService: CaseTagTypeApplyService;
  @Dependencies(QueueService) private QueueService: QueueService;
  @Dependencies(QueueLightService) private QueueLightService: QueueLightService;
  @Dependencies(EntityIdService) private EntityIdService: EntityIdService;
  @Dependencies(RuleEliminateStrategy)
  private RuleEliminateStrategy: RuleEliminateStrategy;
  @Dependencies(NameListEliminateStrategyService)
  private NameListEliminateStrategyService: NameListEliminateStrategyService;

  @State private userData;

  private rules = {
    // relationId: [{ validator: this.$validator.isNumber, trigger: "change" }]
  };

  private ownModel: any = {
    relationId: "",
    applyCategory: "",
    operatorCode: "",
    checkRealName: "",
    applyStatus: ""
  };
  private type: string = "";
  private title: string = "";
  private rowId: string = "";
  private rowName: string = "";
  private rowData: any = {};
  private ownDataSet: any[] = [];
  private dialog: any = { leaveApproval: false };
  private recordHistory: any[] = [];
  private showSelect: boolean = true;
  private rowType: string = "";

  private get userId () {
    return this.userData.id;
  }

  private get isQueue () {
    // 是否队列审批
    if (
      this.rowType === "SELF_QUEUE" ||
      this.rowType === "CHANNEL_QUEUE" ||
      this.rowType === "LITIGATION_QUEUE" ||
      this.rowType === "OUTSOURCE_QUEUE" ||
      this.rowType === "ROBOT_QUEUE"
    ) {
      return true;
    }
    return false;
  }

  private get isPolicy () {
    // 是否策略审批
    if (
      this.rowType === "ID_STRATEGY" ||
      this.rowType === "CASE_DATE_STRATEGY" ||
      this.rowType === "RULE_ELIMINATE" ||
      this.rowType === "NAMELIST_USER_ELIMINATE" ||
      this.rowType === "NAMELIST_ACCOUNT_ELIMINATE"
    ) {
      return true;
    }
    return false;
  }

  private mounted () {
    this.refreshData();
  }

  private approvalClick (scope, title) {
    this.rowType = scope.row.applyCategory;
    this.title = title;
    this.rowId = scope.row.id;
    this.rowName = scope.row.operatorCode;
    let apiType;
    if (this.rowType === "LEAVE_CASE" || this.rowType === 'DERATE_CASE') { // 账户审批 8个类型 留案 减免
      apiType = this.caseApplyRecordService;
    } else if (this.rowType === "CASE_ADJUSTMENT") { // 案件类型调整
      apiType = this.CaseTagTypeApplyService;
    } else if(this.rowType === 'PAUSE_TEL' || this.rowType === 'PAUSE_SMS' || this.rowType === 'STOP_CASE' || this.rowType === 'PAUSE_CASE' || this.rowType === "ACTIVE_SMS" || this.rowType === "ACTIVE_TEL" || this.rowType === "PAUSE_CASE_ACTIVE") { // 停催案件 停催电话 停催短信 停催激活案件 电话 停催短信
      apiType = this.CasePauseActiveService
    } else if(this.rowType === 'AGENT_APPLY') { // 代理人
      apiType = this.CaseInfoService
    } else if(this.rowType === 'CASE_BACK') { // 退案
      apiType = this.UserTagService
    } else if (this.rowType === "TAG_CREATE") { // 标签审批
      apiType = this.UserTagService;
    } else if (this.rowType === "TAG_ACCOUNT") { // 账户标签审批   
      apiType = this.CaseTagAdjustApplyServer;
    } else if (this.rowType === "TAG_USER") { // 账户标签审批
      apiType = this.PersonalTagAdjustApplyServer;
    } else if (this.rowType === "CASE_CREATE_ADJUSTMENT") { // 案件类型调整审批-新建
      apiType = this.CustomerGroupManageService;
    } else if (this.isQueue && this.rowType !== "CHANNEL_QUEUE") { // 队列审批并不是轻渠道
      apiType = this.QueueService;
    } else if (this.isQueue && this.rowType === "CHANNEL_QUEUE") { // 队列审批并是轻渠道
      apiType = this.QueueLightService;
    } else if (this.isPolicy && this.rowType === "ID_STRATEGY") { // 策略审批-实体ID
      apiType = this.EntityIdService;
    } else if (this.isPolicy && this.rowType === "CASE_DATE_STRATEGY") { // 策略审批-分案日期策略
      apiType = this.DateStrategyService;
    } else if (this.isPolicy && this.rowType === "RULE_ELIMINATE") { // 策略审批-规则剔除策略
      apiType = this.RuleEliminateStrategy;
    } else if (this.isPolicy && (this.rowType === "NAMELIST_USER_ELIMINATE" || this.rowType === "NAMELIST_ACCOUNT_ELIMINATE")) { // 策略审批-用户名单剔除 || 策略审批-账户名单剔除
      apiType = this.NameListEliminateStrategyService;
    } else {
      return;
    }
    const params = {};
    if (
      this.isQueue &&
      this.rowType !== "ROBOT_QUEUE" &&
      this.rowType !== "CHANNEL_QUEUE" &&
      this.rowType !== "SELF_QUEUE" &&
      this.rowType !== "LITIGATION_QUEUE" &&
      this.rowType !== "OUTSOURCE_QUEUE"
    ) {
      params['businessKey'] = scope.row.caseTaskId;
    } else if (this.isPolicy) {
      params['id'] = scope.row.caseTaskId;
    } else if (
      this.rowType === "ACTIVE_SMS" ||
      this.rowType === "ACTIVE_TEL" ||
      this.rowType === "LEAVE_CASE" ||
      this.rowType === "PAUSE_TEL" ||
      this.rowType === "PAUSE_SMS"||
      this.rowType === "STOP_CASE" ||
      this.rowType === 'PAUSE_CASE' ||
      this.rowType === "DERATE_CASE" || this.rowType === "ACTIVE_SMS" || this.rowType === "ACTIVE_TEL" || this.rowType === "PAUSE_CASE_ACTIVE"
    ) {
      params['applyId'] = scope.row.id;
      params['id'] = scope.row.id;
      params['caseTaskId'] = scope.row.caseTaskId
      params['caseNumber'] = scope.row.caseNumber
      params['applyCategory'] = scope.row.applyCategory
    } else if(this.rowType === 'AGENT_APPLY') {
      params['caseNumber'] = scope.row.caseNumber
      params['id'] = scope.row.id;
    } else {
      params['applyId'] = scope.row.caseTaskId;
    }
    apiType.getTagApplyById(params).subscribe(data => {
      this.getHistory(scope.row.id);
      this.type = title;
      this.rowData = data;
      this.rowData.checkUserLinkedList = scope.row.checkUserLinkedList;
      this.rowData.applyCategory = scope.row.applyCategory;
      this.rowData.applyStatus = scope.row.applyStatus;
      this.dialog.leaveApproval = true;
    });
  }

  private getHistory (id) {
    // 获取审批历史
    this.caseApplyRecordService
      .recordHistory({ applyId: id, applyCategory: this.rowType })
      .subscribe(data => {
        this.recordHistory = data;
      });
  }

  private refreshData () {
    this.caseApplyRecordService
      .getAllCaseApplyRecords(this.ownModel, this.pageService, {
        applyTime: "desc"
      })
      .subscribe(data => {
        this.ownDataSet = data;
      });
  }
}
</script>

<style lang="less" scoped>
.about-id {
  cursor: pointer;
  color: #409eff;
}
</style>
